﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using JSONPTest;
using JSONPTest.Model;
using JSONPTest.Models;


namespace JSONPTest.Controllers
{
    public class CountiesController : ApiController
    {
        private CountyContext db = new CountyContext();

        // GET: api/Counties
        
        public IQueryable<County> GetCounties()
        {
            List<County> cc = new List<County>();

            County c = new County();
            c.CountyName = "Shawnee";
            c.ID = 1;
            c.Locations = 2;
            cc.Add(c);

            c = new County();
            c.ID = 2;
            c.CountyName = "Allen";
            c.Locations = 1;
            cc.Add(c);

            c = new County();
            c.ID = 3;
            c.CountyName = "Johnson";
            c.Locations = 3;
            cc.Add(c);

            //System.Threading.Thread.Sleep(4000);
            return cc.OrderBy(x => x.CountyName).AsQueryable();
        }

        [Route("api/counties/count")]
        //[HttpGet]
        public IEnumerable<LocationCount> GetCountiesCount()
        {






            List<LocationCount> locs = new List<LocationCount>();
            LocationCount l;

            l = new LocationCount();
            l.LocationName = "shawnee";
            l.Count = 2;
            locs.Add(l);

            l = new LocationCount();
            l.LocationName = "johnson";
            l.Count = 3;
            locs.Add(l);

            l = new LocationCount();
            l.LocationName = "allen";
            l.Count = 1;
            locs.Add(l);


            Location ll = new Location();
            County c = new County();
            GenericTest gt = new GenericTest();
            var returnData = gt.Swap22<County, Location>(c, c, ll);





            return locs;
        }

        
        
        //// GET: api/Counties/5
        //[ResponseType(typeof(County))]
        //public async Task<IHttpActionResult> GetCounty(int id)
        //{
        //    //County county = await db.Counties.FindAsync(id);
        //    //if (county == null)
        //    //{
        //    //    return NotFound();
        //    //}
        //    County county = new County();
        //    county.ID = 123;
        //    county.CountyName = "JSONP Rocks...........";
        //    return Ok(county);
        //}

        // PUT: api/Counties/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutCounty(int id, County county)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != county.ID)
            {
                return BadRequest();
            }

            db.Entry(county).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CountyExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Counties
        [ResponseType(typeof(County))]
        public async Task<IHttpActionResult> PostCounty(County county)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Counties.Add(county);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = county.ID }, county);
        }

        // DELETE: api/Counties/5
        [ResponseType(typeof(County))]
        public async Task<IHttpActionResult> DeleteCounty(int id)
        {
            County county = await db.Counties.FindAsync(id);
            if (county == null)
            {
                return NotFound();
            }

            db.Counties.Remove(county);
            await db.SaveChangesAsync();

            return Ok(county);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool CountyExists(int id)
        {
            return db.Counties.Count(e => e.ID == id) > 0;
        }
    }
}